# -*- coding: utf-8 -*-
__author__ = 'Paul'


from plone.app.layout.globals.interfaces import IViewView
from Products.bpTheme.interfaces import IBpTheme
from zope.interface import Interface
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from plone.app.layout.globals.interfaces import IViewView
from Products.ATContentTypes.interface import IATFolder
from zope.site.hooks import getSite
from zope.component import getMultiAdapter



from Acquisition import aq_inner
from zope.component import getUtility
from zope.intid.interfaces import IIntIds
from zope.security import checkPermission
from zc.relation.interfaces import ICatalog


# Import default Plone viewlet classes
from plone.app.layout.viewlets              import common as base

class RelacionadoViewlet(base.ViewletBase):


    def muestroRelaciones(self):
        from Products.CMFCore.utils import getToolByName
        from Products.bpContents.getImgTira import GetTiraImgs

        try:
            films = []
            if self.context.relaciones is not None:
                for ref in self.context.relaciones:
                    obj = ref.to_object
                    objExp=GetTiraImgs(obj)
                    if objExp.getImagenDestacada()!=None:
                        destacada=objExp.getImagenDestacada()[0]
                        urlImageURL=destacada["url"] or False
                        urlImageDes=destacada["title"] or False
                        films.append({
                            'url': obj.absolute_url(),
                            'title': obj.title,
                            'summary': obj.description,
                            'imageUrl': urlImageURL,
                            'imageTitle': urlImageDes,
                            })
                    else:
                        films.append({
                            'url': obj.absolute_url(),
                            'title': obj.title,
                            'summary': obj.description,
                            'imageUrl': None,
                            'imageTitle': None,
                            })

            if len(films)>0:
                return films
            else:
                return False
        except:
            return None




    def back_references(self):
        """ Return back references from source object on specified attribute_name """

        catalog = getUtility(ICatalog)
        intids = getUtility(IIntIds)
        result = []

        for rel in catalog.findRelations({"to_id":intids.getId(self.context)}):
            obj = intids.queryObject(rel.from_id)
            if obj is not None and checkPermission('zope2.View', obj):
                result.append(obj)

        if len(result)>0:
            return result
        else:
            return False

    """
          <tal:back tal:condition="viewlet/back_references">

        <ul class="backReference" >
            <li tal:repeat="item viewlet/back_references">
                <a href="" tal:attributes="href item/absolute_url;
                                           title item/description"
                            tal:content="item/title"></a>
            </li>
        </ul>
        </tal:back>
    """